//
//  visitorView.swift
//  MySinaWeiBo
//
//  Created by zhangfuwei on 15/7/29.
//  Copyright © 2015年 zhangfuwei. All rights reserved.
//

import UIKit

///  访问视图协议
protocol visitorViewDelegate :NSObjectProtocol {
    func willResgin()
    func willLogin()
    
}


class visitorView: UIView {

    weak var delegate : visitorViewDelegate?
    
    ///  注册,登录按钮点击事件
    func clickRegister() {

        delegate?.willResgin()
    }
     func clickLogin() {

        delegate?.willLogin()
    }

    
    ///  创建 view 会调用此方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        //  禁止从 sb/ xib 使用本类
        fatalError("init(coder:) has not been implemented")
    }
    
    ///  设置视图的信息
     func setViewInfo(isHomeView :Bool , imageName: String , title: String) {
        iconView.image = UIImage(named: imageName)
        messageLbl.text = title
        
        homeView.hidden = !isHomeView
        isHomeView ? startAnimation() : sendSubviewToBack(maskIconView)
        
    }

    //  开启核心动画
    func startAnimation() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.toValue = M_PI * 2
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
    
    
    //  添加自控件函数
    func setupUI() {
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeView)
        addSubview(messageLbl)
        addSubview(resignBtn)
        addSubview(loginBtn)
        
        //  1.设置iconView约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //  2.设置 homeView 约束
        homeView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
       
        //  3.消息文字
        messageLbl.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLbl, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLbl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLbl, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        //  4.注册按钮
        resignBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: resignBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: resignBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: -0))
        addConstraint(NSLayoutConstraint(item: resignBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 100))
        addConstraint(NSLayoutConstraint(item: resignBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 35))
        
        //  5.登录按钮
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 35))
        //  6.设置 maskiconView 约束
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-(-35)-[regButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": maskIconView, "regButton": resignBtn]))
        
        //  7.美工提供的素材大多是纯色的,所以可以用这个测素计来计算颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)

    }
    
    
    // MARK : - 懒加载视图控件
    ///  大图
    lazy var iconView :UIImageView = {
       
        let view = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return view
        
    }()
    ///  遮罩
    lazy var maskIconView : UIImageView = {
        
        let view = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return view
        
    }()
    
    
    /// 小房子
    lazy var homeView :UIImageView = {
        
        let hView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return hView
    }()
    
    ///  消息文本框
    lazy var messageLbl :UILabel = {
        let lbl = UILabel()
        
        lbl.text = "关注一些人，回这里看看有什么惊喜"
        lbl.textColor = UIColor.darkGrayColor()
        lbl.numberOfLines = 0
        lbl.font = UIFont.systemFontOfSize(14)
        lbl.textAlignment = NSTextAlignment.Center
        lbl.sizeToFit()
        return lbl
    }()


    ///  注册按钮
    lazy var resignBtn : UIButton = {
       
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    
    ///  登录按钮
    
    lazy var loginBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
   
}
